package com.zrx.app.android.fragment.calculators;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.zrx.app.android.R;

import org.w3c.dom.Text;

import java.text.DecimalFormat;
/*中融信2号计算页面*/
public class Cal1Frag extends CalBase {

    public static Cal1Frag newInstance() {
        Cal1Frag fragment = new Cal1Frag();
        return fragment;
    }

    EditText transactionPriceText, gpsText, otherFeeText;
    TextView coefLoanRatioText,coef1_2yrsText,coef1_3yrsText,coef2yrsInterestRateText,coef3yrsInterestRateText;
    TextView out2yrsMonthPaymentText,out3yrsMonthPaymentText
            ,out2yrsLoanText,out3yrsLoanText
            ,out2yrsFinalPaymentText,out3yrsFinalPaymentText
            ,out2yrsInterestText,out3yrsInterestText
            ,out2yrsDownPrinciplePaymentText,out3yrsDownPrinciplePaymentText
            ,out2yrsDownPaymentText,out3yrsDownPaymentText
            ,out2yrsSumPaymentText,out3yrsSumPaymentText
            ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cal1, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        MyTextWatcher myTextWatcher=new MyTextWatcher();

        //input
        transactionPriceText = (EditText) view.findViewById(R.id.cal1_transaction_price);
        transactionPriceText.addTextChangedListener(myTextWatcher);
        gpsText = (EditText) view.findViewById(R.id.cal1_gps_fee);
        gpsText.addTextChangedListener(myTextWatcher);
        otherFeeText = (EditText) view.findViewById(R.id.cal1_other_fee);
        otherFeeText.addTextChangedListener(myTextWatcher);

        //coef
        coefLoanRatioText = (TextView) view.findViewById(R.id.cal1_loan_ratio);
        coef1_2yrsText = (TextView) view.findViewById(R.id.cal1_2year_coef1);
        coef1_3yrsText = (TextView) view.findViewById(R.id.cal1_3year_coef1);
        coef2yrsInterestRateText = (TextView) view.findViewById(R.id.cal1_2year_interest_rate);
        coef3yrsInterestRateText = (TextView) view.findViewById(R.id.cal1_3year_interest_rate);

        //out
        out2yrsMonthPaymentText = (TextView) view.findViewById(R.id.cal1_2year_monthly_payment);
        out3yrsMonthPaymentText = (TextView) view.findViewById(R.id.cal1_3year_monthly_payment);
        out2yrsLoanText = (TextView) view.findViewById(R.id.cal1_2year_loan);
        out3yrsLoanText = (TextView) view.findViewById(R.id.cal1_3year_loan);
        out2yrsFinalPaymentText = (TextView) view.findViewById(R.id.cal1_2year_final_payment);
        out3yrsFinalPaymentText = (TextView) view.findViewById(R.id.cal1_3year_final_payment);
        out2yrsInterestText = (TextView) view.findViewById(R.id.cal1_2year_interest);
        out3yrsInterestText = (TextView) view.findViewById(R.id.cal1_3year_interest);
        out2yrsDownPrinciplePaymentText = (TextView) view.findViewById(R.id.cal1_2year_down_principle_payment);
        out3yrsDownPrinciplePaymentText = (TextView) view.findViewById(R.id.cal1_3year_down_principle_payment);
        out2yrsDownPaymentText = (TextView) view.findViewById(R.id.cal1_2year_down_payment);
        out3yrsDownPaymentText = (TextView) view.findViewById(R.id.cal1_3year_down_payment);
        out2yrsSumPaymentText = (TextView) view.findViewById(R.id.cal1_2year_sum_payment);
        out3yrsSumPaymentText = (TextView) view.findViewById(R.id.cal1_3year_sum_payment);

        return view;
    }



    void calculate(){
        try{
            //input
            double transactionPrice=Double.valueOf(transactionPriceText.getText().toString());
            double gps=Double.valueOf(gpsText.getText().toString());
            double otherFee=Double.valueOf(otherFeeText.getText().toString());

            //coef
            double coefLoanRatio=Double.valueOf(coefLoanRatioText.getText().toString());
            double coef1_2yrs=Double.valueOf(coef1_2yrsText.getText().toString());
            double coef1_3yrs=Double.valueOf(coef1_3yrsText.getText().toString());
            double coef2yrsInterestRate=Double.valueOf(coef2yrsInterestRateText.getText().toString());
            double coef3yrsInterestRate=Double.valueOf(coef3yrsInterestRateText.getText().toString());

            //out
            double monthlyPayment2yrs=transactionPrice*coefLoanRatio*coef1_2yrs;
            double monthlyPayment3yrs=transactionPrice*coefLoanRatio*coef1_3yrs;

            double loan2yrs=24*monthlyPayment2yrs;
            double loan3yrs=36*monthlyPayment3yrs;

            double interest2yrs=24*loan2yrs*coef2yrsInterestRate;
            double interest3yrs=36*loan3yrs*coef3yrsInterestRate;

            double downPrinciplePayment2yrs=transactionPrice-loan2yrs;
            double downPrinciplePayment3yrs=transactionPrice-loan3yrs;

            double downPayment2yrs=downPrinciplePayment2yrs+interest2yrs;
            double downPayment3yrs=downPrinciplePayment3yrs+interest3yrs;

            double finalPayment2yrs=transactionPrice-downPayment2yrs;
            double finalPayment3yrs=transactionPrice-downPayment3yrs;

            double sumPayment2yrs=downPayment2yrs+gps+otherFee;
            double sumPayment3yrs=downPayment3yrs+gps+otherFee;

            out2yrsMonthPaymentText.setText(nf.format(monthlyPayment2yrs));
            out3yrsMonthPaymentText .setText(nf.format(monthlyPayment3yrs));
            out2yrsLoanText .setText(nf.format(loan2yrs));
            out3yrsLoanText .setText(nf.format(loan3yrs));
            out2yrsFinalPaymentText .setText(nf.format(finalPayment2yrs));
            out3yrsFinalPaymentText .setText(nf.format(finalPayment3yrs));
            out2yrsInterestText .setText(nf.format(interest2yrs));
            out3yrsInterestText.setText(nf.format(interest3yrs));
            out2yrsDownPrinciplePaymentText .setText(nf.format(downPrinciplePayment2yrs));
            out3yrsDownPrinciplePaymentText .setText(nf.format(downPrinciplePayment3yrs));
            out2yrsDownPaymentText.setText(nf.format(downPayment2yrs));
            out3yrsDownPaymentText .setText(nf.format(downPayment3yrs));
            out2yrsSumPaymentText .setText(nf.format(sumPayment2yrs));
            out3yrsSumPaymentText .setText(nf.format(sumPayment3yrs));
        }catch (Exception e){

        }

    }

}
